/*
** Graphics.cpp for 304Pacman in /home/mickael/test
**
** Made by mickael gomes
** Login   <gomes.mickael@epitech.net>
**
** Started on  ven. nov.  08 13:49:13 2013 mickael gomes
** Last update ven. nov.  08 13:49:13 2013 mickael gomes
*/

#include                        <iostream>
#include                        <sstream>
#include                        "Graphics.hh"

Graphics::Graphics(std::vector< std::pair<int*, bool> > map, int* ghostCoord, int x, int y, std::vector<int*> path)
    : map_(map), ghostCoord_(ghostCoord), x_(x), y_(y), window_(new sf::RenderWindow(sf::VideoMode(x_ * 50, y_ * 25), "My window")), path_(path)
{
    font_.loadFromFile("arial.ttf");
    text_.setFont(font_);
    text_.setCharacterSize(24);
    text_.setColor(sf::Color::White);
    drawMap();
    while (window_->isOpen())
    {
        sf::Event event;
        while (window_->pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window_->close();
        }
        window_->clear();
        for (std::vector<>::iterator it = rect_.begin(); it != rect_.end(); it++)
            window_->draw(*it);
        window_->display();
    }
}

Graphics::~Graphics()
{
	
}

bool                        Graphics::checkValidPath(int x, int y)
{
    std::vector<int*>::iterator it = path_.begin();

    std::cout << "VPATH" << std::endl;
    while (it != path_.end())
    {
        if (x == (*it)[1] && y == (*it)[0])
            return true;
        it++;
    }
    return false;
}

void                        Graphics::drawShortestPath(int x, int y, int xscreen, int yscreen)
{
    std::cout << "SPATH" << std::endl;
    std::ostringstream      s;
    sf::RectangleShape rectangle;

    s << map_[x].first[y];
    std::string             str(s.str());
    text_.setString(str);
    text_.setPosition(xscreen, yscreen);
    text_.setColor(sf::Color::Black);
    window_->draw(text_);
    rectangle.setSize(sf::Vector2f(50, 25));
    rectangle.setFillColor(sf::Color::Yellow);
    rectangle.setOutlineColor(sf::Color::Black);
    rectangle.setOutlineThickness(1);
    rectangle.setPosition(xscreen, yscreen);
    //window_->draw(rectangle);
    //window_->draw(text_);
    rect_.pushback(rectangle);
    rect_.pushback(text_);
    text_.setColor(sf::Color::White);
}

void                        Graphics::drawNumber(int x, int y, int xscreen, int yscreen)
{
    std::ostringstream      s;

    std::cout << "Number" << std::endl;
    if (checkValidPath(x, y))
        return (drawShortestPath(x, y, xscreen, yscreen));
    if (map_[x].first[y] != 0)
    {
        s << map_[x].first[y];
        std::string             str(s.str());

        text_.setString(str);
        text_.setPosition(xscreen, yscreen);
  //      window_->draw(text_);
        rect_.pushback(text_);
    }
}

void                        Graphics::drawTexture(int x, int y, std::string path)
{
    sf::Texture             texture;
    sf::Sprite              sprite;

    std::cout << "Texture" << std::endl;
    texture.loadFromFile(path);
    sprite.setTexture(texture);
    if (path == "pacman.png")
        sprite.setScale(0.11, 0.1);
    else
        sprite.setScale(0.02, 0.02);
    sprite.setPosition(x, y);
//    window_->draw(sprite);
    rect_.pushback(sprite);
}

void                        Graphics::drawWall(int x, int y)
{
    std::cout << "Wall" << std::endl;
    sf::RectangleShape rectangle;

    rectangle.setSize(sf::Vector2f(50, 25));
    rectangle.setFillColor(sf::Color::White);
    rectangle.setOutlineColor(sf::Color::Black);
    rectangle.setOutlineThickness(1);
    rectangle.setPosition(x, y);
//    window_->draw(rectangle);
    rect_.pushback(rectangle);
}

void                        Graphics::drawMap()
{
    int                     xmap, ymap = 0;
    int                     xscreen, yscreen = 0;

    while (yscreen != y_ *25 && xscreen != x_ * 50)
    {
        if (map_[xmap].first[ymap] == -1)
            drawWall(xscreen, yscreen);
        else if (map_[xmap].first[ymap] == -2)
            drawTexture(xscreen, yscreen, "ghost.png");
        else if (map_[xmap].first[ymap] == -3)
            drawTexture(xscreen, yscreen, "pacman.png");
        else
            drawNumber(xmap, ymap, xscreen, yscreen);
        xscreen += 50;
        ymap++;
        if (xscreen == x_ * 50)
        {
            ymap = 0;
            xmap++;
            yscreen += 25;
            xscreen = 0;
        }
    }
    std::cout << "end" << std::endl;
}